<?php
/**
 * File: Base.php
 * User: Mr.Yang
 * Date: 2025/3/22 0022
 * Time: 13:24
 * Note:
 */

namespace app\home\controller;


use app\admin\model\Area;
use think\Controller;
use app\admin\model\Cart as CartModel;
use app\admin\model\Setting as SettingModel;

class Base extends Controller
{
    protected $url = null;
    public function initialize()
    {
        parent::initialize();

        $controller = request()->controller();
        $action = request()->action();

        $url = $controller.'/'.$action;
        $this->url = $url;
        $this->assign('url', $url);
        $this->assign('cart_num', $this->getCartNumber());

        $settingModel = new SettingModel();
        $setting = $settingModel->getList('base');
        $this->assign('setting', $setting);
    }

    /**
     * Note: 检查登录情况
     * User: Mr.Yang
     */
    protected function checkLogin()
    {
        if (!session('userid')) {
            $this->redirect(url('user/login'));
        }
    }

    /**
     * Note: 获取我的购物的数
     * User: Mr.Yang
     * @return $this|int
     */
    protected function getCartNumber()
    {
        $uid = session('userid');
        if ($uid) {
            //获取我的购物车数量
            $cart_num = CartModel::where('userid', '=', $uid)->count();
            return $cart_num;
        }
        return 0;
    }

    /**
     * Note: 获取地区
     * User: Mr.Yang
     * @param int $pid
     * @return $this
     */
    protected function getAreaById($id = 0)
    {
        $area = Area::where('areaid', '=', $id)->find();
        return $area;
    }

    /**
     * Note: 获取地区
     * User: Mr.Yang
     * @param $areaid
     * @param string $str
     * @param int $deep
     * @return bool|string
     */
    protected function area_pos($areaid, $str = ' &raquo; ', $deep = 0)
    {
        $areaid = intval($areaid);
        $arrparentid = Area::where('areaid', '=', $areaid)->column('arrparentid');
        $arrparentid = $arrparentid ? explode(',', $arrparentid[0]) : array();
        $arrparentid[] = $areaid;
        $pos = '';
        if ($deep) $i = 1;
        foreach ($arrparentid as $areaid) {
            $a = Area::where('areaid', '=', $areaid)->find();
            if (!$areaid || !$a) continue;
            if ($deep) {
                if ($i > $deep) continue;
                $i++;
            }
            $pos .= $a['areaname'] . $str;
        }
        $_len = strlen($str);
        if ($str && substr($pos, -$_len, $_len) === $str) $pos = substr($pos, 0, strlen($pos) - $_len);
        return $pos;
    }

    /**
     * Note: 获取购物车和金额
     * User: Mr.Yang
     * @param $uid
     * @param $cartid
     * @return array
     */
    protected function getCartAndTotal($uid, $cartid)
    {
        //获取选中的购物车商品列表
        $cart = CartModel::field('c.*, b.bname, b.thumb, b.price, b.total')
            ->alias('c')
            ->leftJoin('book b', 'b.id=c.bookid')
            //->where(['c.userid' => $uid, 'c.id' => explode(',', $cartid)])
            ->where([['c.userid', '=', $uid], ['c.id', 'in', $cartid]])
            ->order('c.id', 'desc')
            ->select()->toArray();

        $total = 0;
        foreach ($cart as $k => $v) {
            $money = $v['price'] * $v['number'];
            $cart[$k]['money'] = $money;
            $total += $money;
        }
        return [$cart, $total];
    }
}